#!/usr/bin/env bash

# 阿拉伯数字18作为异常退出码
check='[ $? -eq 0 ] || exit 18'

version=`python3 --version`
if [[ $? -ne 0 || $version != 'Python 3.8'* ]];
then
  eval "$check" && echo '安装python:3.8'
  eval "$check" && echo 'python:3.8.2安装包下载、解压、编译、安装'
  if [ ! -d "Python-3.8.2" ];then
    if [ ! -f "Python-3.8.2.tgz" ];then
      eval "$check" && echo '下载python-3.8.2'
      eval "$check" && wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
    else
      echo "文件 Python-3.8.2.tgz 已存在"
    fi
    eval "$check" && echo '解压Python-3.8.2.tgz'
    eval "$check" && tar -zxvf Python-3.8.2.tgz
  else
    echo "文件夹 Python-3.8.2 已存在"
  fi
  eval "$check" && echo '进入目录Python-3.8.2'
  eval "$check" && cd Python-3.8.2
  eval "$check" && echo '配置、编译和安装'
  eval "$check" && ./configure --enable-optimizations
  eval "$check" && make
  eval "$check" && make install
  eval "$check" && cd ..
  eval "$check" && echo '安装venv'
  eval "$check" && apt install -y python3-venv
  # 暂时先不删除
  #eval "$check" && rm -rf Python-3.8.2*
else
  echo '已经安装了python:$(version)'
  echo '本项目代码基于python:3.8，其他版本可能导致某些功能无法正常使用'
  echo '如不能使用，可手动安装python:3.8，或者卸载python3后重新安装。'
fi

eval "$check" && echo "python安装完成！！！"
